<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息管理系统 - API测试页面</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
            padding: 30px;
        }
        
        h1 {
            color: #667eea;
            text-align: center;
            margin-bottom: 10px;
            font-size: 32px;
        }
        
        .subtitle {
            text-align: center;
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }
        
        .section {
            margin-bottom: 30px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #667eea;
        }
        
        .section h2 {
            color: #333;
            margin-bottom: 15px;
            font-size: 20px;
        }
        
        .btn-group {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 15px;
        }
        
        button {
            padding: 12px 24px;
            background: #667eea;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s;
            font-weight: 500;
        }
        
        button:hover {
            background: #5568d3;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }
        
        button:active {
            transform: translateY(0);
        }
        
        .result {
            background: #fff;
            padding: 15px;
            border-radius: 6px;
            border: 1px solid #ddd;
            margin-top: 15px;
            max-height: 400px;
            overflow-y: auto;
        }
        
        .result pre {
            margin: 0;
            color: #333;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 13px;
            line-height: 1.6;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        
        .success {
            color: #28a745;
            font-weight: bold;
        }
        
        .error {
            color: #dc3545;
            font-weight: bold;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }
        
        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            border: 2px solid #667eea;
        }
        
        .stat-card h3 {
            color: #667eea;
            font-size: 32px;
            margin-bottom: 5px;
        }
        
        .stat-card p {
            color: #666;
            font-size: 14px;
        }
        
        .footer {
            text-align: center;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #ddd;
            color: #666;
            font-size: 12px;
        }
        
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-left: 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🎓 学生信息管理系统</h1>
        <p class="subtitle">AI辅助开发 - 后端API测试页面</p>
        
        <div class="section">
            <h2>📊 系统状态</h2>
            <div class="btn-group">
                <button onclick="checkHealth()">检查服务状态</button>
            </div>
            <div id="healthResult" class="result" style="display:none;"></div>
        </div>
        
        <div class="section">
            <h2>📝 学生信息管理</h2>
            <div class="btn-group">
                <button onclick="getStudents()">获取学生列表</button>
                <button onclick="createSampleStudent()">创建示例学生</button>
                <button onclick="getStatistics()">获取成绩统计</button>
                <button onclick="generateAIReport()">生成AI分析报告</button>
            </div>
            <div id="studentsResult" class="result" style="display:none;"></div>
        </div>
        
        <div class="section">
            <h2>🤖 AI分析功能展示</h2>
            <div id="statsDisplay" class="stats-grid" style="display:none;"></div>
            <div id="aiResult" class="result" style="display:none;"></div>
        </div>
        
        <div class="footer">
            <p>AI辅助学生信息管理系统 | 技术栈: React + Express + MongoDB | 开发工具: Qoder AI</p>
            <p>大作业项目 - 2025年10月</p>
        </div>
    </div>

    <script>
        const API_BASE = 'http://localhost:3001';

        async function checkHealth() {
            const resultDiv = document.getElementById('healthResult');
            resultDiv.style.display = 'block';
            resultDiv.innerHTML = '<div class="loading"></div> 检查中...';
            
            try {
                const response = await fetch(`${API_BASE}/health`);
                const data = await response.json();
                resultDiv.innerHTML = `<pre class="success">✅ 服务器运行正常\n状态: ${data.status}\n时间: ${data.timestamp}</pre>`;
            } catch (error) {
                resultDiv.innerHTML = `<pre class="error">❌ 连接失败: ${error.message}\n请确保后端服务正在运行（localhost:3001）</pre>`;
            }
        }

        async function getStudents() {
            const resultDiv = document.getElementById('studentsResult');
            resultDiv.style.display = 'block';
            resultDiv.innerHTML = '<div class="loading"></div> 加载中...';
            
            try {
                const response = await fetch(`${API_BASE}/api/students?limit=5`);
                const data = await response.json();
                
                if (data.success) {
                    let html = `<pre class="success">✅ 查询成功\n总数: ${data.data.pagination.total} 条记录\n\n`;
                    html += '学生列表:\n';
                    data.data.students.forEach((student, index) => {
                        html += `${index + 1}. ${student.name} (${student.studentNo}) - ${student.className} - 成绩: ${student.grade}分\n`;
                    });
                    html += '</pre>';
                    resultDiv.innerHTML = html;
                } else {
                    resultDiv.innerHTML = `<pre class="error">❌ ${data.message}</pre>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<pre class="error">❌ 请求失败: ${error.message}</pre>`;
            }
        }

        async function createSampleStudent() {
            const resultDiv = document.getElementById('studentsResult');
            resultDiv.style.display = 'block';
            resultDiv.innerHTML = '<div class="loading"></div> 创建中...';
            
            const randomNo = '2025' + Math.floor(Math.random() * 10000).toString().padStart(4, '0');
            const student = {
                studentNo: randomNo,
                name: '测试学生' + Math.floor(Math.random() * 100),
                className: '计算机一班',
                grade: Math.floor(Math.random() * 41) + 60
            };
            
            try {
                const response = await fetch(`${API_BASE}/api/students`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(student)
                });
                const data = await response.json();
                
                if (data.success) {
                    resultDiv.innerHTML = `<pre class="success">✅ 创建成功
学号: ${data.data.studentNo}
姓名: ${data.data.name}
班级: ${data.data.className}
成绩: ${data.data.grade}分</pre>`;
                } else {
                    resultDiv.innerHTML = `<pre class="error">❌ ${data.message}</pre>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<pre class="error">❌ 请求失败: ${error.message}</pre>`;
            }
        }

        async function getStatistics() {
            const statsDiv = document.getElementById('statsDisplay');
            const resultDiv = document.getElementById('studentsResult');
            resultDiv.style.display = 'block';
            resultDiv.innerHTML = '<div class="loading"></div> 统计中...';
            
            try {
                const response = await fetch(`${API_BASE}/api/students/analysis/statistics`);
                const data = await response.json();
                
                if (data.success) {
                    const stats = data.data.basic;
                    const dist = data.data.distribution;
                    
                    statsDiv.style.display = 'grid';
                    statsDiv.innerHTML = `
                        <div class="stat-card">
                            <h3>${stats.totalStudents}</h3>
                            <p>学生总数</p>
                        </div>
                        <div class="stat-card">
                            <h3>${stats.avgGrade.toFixed(1)}</h3>
                            <p>平均分</p>
                        </div>
                        <div class="stat-card">
                            <h3>${stats.maxGrade}</h3>
                            <p>最高分</p>
                        </div>
                        <div class="stat-card">
                            <h3>${stats.minGrade}</h3>
                            <p>最低分</p>
                        </div>
                    `;
                    
                    let html = '<pre class="success">✅ 统计完成\n\n成绩分布:\n';
                    html += `优秀(90-100): ${dist.excellent.count}人 (${dist.excellent.percentage}%)\n`;
                    html += `良好(80-89): ${dist.good.count}人 (${dist.good.percentage}%)\n`;
                    html += `及格(60-79): ${dist.pass.count}人 (${dist.pass.percentage}%)\n`;
                    html += `不及格(0-59): ${dist.fail.count}人 (${dist.fail.percentage}%)</pre>`;
                    resultDiv.innerHTML = html;
                } else {
                    statsDiv.style.display = 'none';
                    resultDiv.innerHTML = `<pre class="error">❌ ${data.message}</pre>`;
                }
            } catch (error) {
                statsDiv.style.display = 'none';
                resultDiv.innerHTML = `<pre class="error">❌ 请求失败: ${error.message}</pre>`;
            }
        }

        async function generateAIReport() {
            const aiDiv = document.getElementById('aiResult');
            aiDiv.style.display = 'block';
            aiDiv.innerHTML = '<div class="loading"></div> AI分析中...';
            
            try {
                const response = await fetch(`${API_BASE}/api/students/analysis/ai-report`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ className: '计算机一班' })
                });
                const data = await response.json();
                
                if (data.success) {
                    const analysis = data.data.analysis;
                    let html = '<pre class="success">🤖 AI分析报告\n\n';
                    html += `班级: ${data.data.className}\n`;
                    html += `总体评价: ${analysis.overallAssessment}\n\n`;
                    html += `平均分: ${analysis.averageScore}\n`;
                    html += `最高分: ${analysis.highestScore}\n`;
                    html += `最低分: ${analysis.lowestScore}\n`;
                    html += `总人数: ${analysis.totalStudents}\n\n`;
                    html += 'AI建议:\n';
                    analysis.suggestions.forEach((s, i) => {
                        html += `${i + 1}. ${s}\n`;
                    });
                    html += '</pre>';
                    aiDiv.innerHTML = html;
                } else {
                    aiDiv.innerHTML = `<pre class="error">❌ ${data.message}</pre>`;
                }
            } catch (error) {
                aiDiv.innerHTML = `<pre class="error">❌ 请求失败: ${error.message}</pre>`;
            }
        }

        // 页面加载时自动检查服务状态
        window.onload = function() {
            checkHealth();
        };
    </script>
</body>
</html>
